/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.extensionpoints.palette;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IConfigurationElement;

import com.cea.papyrus.core.extensionpoints.AbstractExtensionPoint;
import com.cea.papyrus.core.utils.Debug;

/**
 * 
 */
public class Visibility extends AbstractExtensionPoint {
	
	/**
	 * 
	 */
	public final IConfigurationElement	configElement;
	
	/**
	 * 
	 */
	private final Diagram[] diagrams;
	
	/**
	 * 
	 */
	private final Profile[] profiles;
	
	/**
	 * 
	 * 
	 * @param ordinal 
	 * @param configElt 
	 * 
	 * @return 
	 */
	public static Visibility parse(IConfigurationElement configElt, int ordinal) {
		
		if (!configElt.getName().equals(IConstants.TAG_VISIBILITY)) {
			return null;
		}
		
		try {
			return new Visibility(configElt);
			
		} catch (Exception e) {
			
			String msg = "Failed to load visibility in "
							+ configElt
								.getDeclaringExtension()
								.getExtensionPointUniqueIdentifier();
			
			Debug.debug(null, msg, channel);
											
			return null;
		}
	}
	
	/**
	 * 
	 * 
	 * @param ordinal 	 * @param configElt 
	 */
	public Visibility(IConfigurationElement configElt) {
		this.configElement = configElt;
		
		// Retrieving diagrams
		IConfigurationElement[] diagChildren = configElt.getChildren(IConstants.TAG_DIAGRAM);
		if (diagChildren == null) {
			diagrams = null;
		} else {
			List<Diagram> dList = new ArrayList<Diagram>();
			// Read properties
			for (int i = 0 ; i < diagChildren.length ; i++) {
				Diagram proxy = new Diagram(diagChildren[i]);

				if (proxy != null) {
					dList.add(proxy);
				}
			} // end of configElements loop	

			diagrams = dList.toArray(new Diagram[dList.size()]);	
		}
		
		// Retrieving profiles
		IConfigurationElement[] profChildren = configElt.getChildren(IConstants.TAG_PROFILE);
		if (profChildren == null) {
			profiles = null;
		} else {
			List<Profile> pList = new ArrayList<Profile>();
			// Read properties
			for (int i = 0 ; i < profChildren.length ; i++) {
				Profile proxy = new Profile(profChildren[i]);

				if (proxy != null) {
					pList.add(proxy);
				}
			} // end of configElements loop	

			profiles = pList.toArray(new Profile[pList.size()]);	
		}
	}
	
	/**
	 * Default constructor.
	 * 
	 * @param uri 	 */
	public Visibility() {
		this.configElement = null;
		
		diagrams = null;
		profiles = null;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public Diagram[] getDiagrams() {
		return diagrams;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public Profile[] getProfiles() {
		return profiles;
	}
	
}
